$('.delete-message-btn').click(function(){
	notice_id = $(this).parent().find('.notice-id').text();	
	$(this).parent().parent().parent().parent().parent().hide('fast');
	$.ajax({
		type: 'POST',
		url: base_url_feeds+'feeds/delete_message', 
		data: {
			notice_id: notice_id
		}, 
		success: function(data){
			if(data=="deleted"){
				popupwindow("-3人品");
				$('#rp-record').text(parseInt($('#rp-record').text())+parseInt(-3));
			}
		},
		dataType: 'text',  
		cache: false
	});	
})

$('.reply-btn').click(function(){
	if($('#used').text()==0){
		alert("权限不足。验证邮箱并完善你的个人信息之后方可开启留言评论功能。");	
		return false;
	}
	thread_id = $(this).parent().find('.thread-id').text();	
	sender_name = $(this).parent().find('.sender-name').text();	
	sender_id = $(this).parent().find('.sender-id').text();	
	$('#reply-form-holder-'+thread_id).show();
	$('#reply-form-holder-'+thread_id).find('.target-id').text(sender_id);
	$('#reply-form-holder-'+thread_id).find('textarea').attr('placeholder', '回复 '+sender_name);
	$('#reply-form-holder-'+thread_id).find('textarea').focus();
	$('#reply-form-holder-'+thread_id).find('.send-reply-btn').attr('disabled', '');
	$('#reply-form-holder-'+thread_id).find('.send-reply-btn').attr('disabled', '');
})

$('.send-reply-btn').click(function(){
	if($('#used').text()==0){
		alert("权限不足。验证邮箱并完善你的个人信息之后方可开启留言评论功能。");	
		return false;
	}
	if($(this).attr('disabled')!=null){return false;}  
	if($(this).parent().parent().find('.reply-message').val()!=''){
		loading = $(this).parent().find('.send-message-loading');
		loading.css('display','inline-block');
		thread_id = $(this).parent().find('.thread-id').text();		
		message = $(this).parent().parent().find('.reply-message').val();
		noted_user_id = $(this).parent().find('.target-id').text();	
		private = $(this).parent().find('input[type="checkbox"]').is(":checked");
		$(this).parent().parent().find('.reply-message').val('');	
		priv = private?1:0; 
		$.ajax({
			type: 'POST',
			url: base_url_feeds+'feeds/reply', 
			data: {
				thread_id: thread_id,	
				noted_user_id: noted_user_id,
				private: priv,
				message: message,
				},
			success: function(data){
					loading.hide();
					obj = JSON.parse(data);
					reply ='<div class="row collpase feed-reply"><div>'+
										'<a href="'+base_url_user+'profile/u/'+obj.sender_id+'"><img class="left" src="'+obj.sender_image+'"></a>'+
										'<div class="name">'+
											'<span class="message">'+
												'<a href="'+base_url_user+'profile/u/'+obj.sender_id+'"><b>'+obj.sender_name+'</b></a>&nbsp;';
					if(obj.private==1){
						reply = reply + '<img title="悄悄话" src="'+base_url+'assets/img/private.png">';	
					}
					reply = reply + '回复 '+
												obj.target_name+'：'+obj.message+'</span>'+
											'<span>'+
												'<a class="delete-message-btn">删除</a>'+
												'<div class="hidden notice-id">'+obj.id+'</div>'+
											'</span>'+
										'</div>'+
									'</div></div>'+
									'<script>$(\'.delete-message-btn\').click(function(){notice_id = $(this).parent().find(\'.notice-id\').text();	$(this).parent().parent().parent().parent().hide(\'fast\');$.ajax({type: \'POST\',url: base_url_feeds+\'feeds/delete_message\', data: {notice_id: notice_id}, dataType: \'text\',  cache: false});	})</script>'; 
					$('#reply-form-holder-'+thread_id).hide();
					$('#feed-reply-holder-'+thread_id).append(reply);
				
			}, 
			dataType: 'text',  
			cache: false
			});
		}
})
